<?php

use Hanson\Vbot\Message\Text;
require_once 'Idiom.php';

/**
 * Created by PhpStorm.
 * User: 35906
 * Date: 18.11.6
 * Time: 21:52
 */

class Group
{
    use Idiom;//加载成语接龙模块

    public $nickname;
    public $username;

    protected $_lastChater = ['username'=>'','time'=>0];//[username,time]

    protected $message;

    public function __construct(string$nickname)
    {
        $this->nickname = $nickname;
    }

    public function dealCase($message){
        $this->message = $message;
        if($this->isNewSD()){$this->welcomeNewSD();}
        elseif ($this->whenInGame()){$this->dealGame();}
        elseif ($this->isStartGame()){$this->startGame();}
        elseif($this->isSDChatMe()){$this->chatWithSD();}
    }


    /*************新人入群**************/
    protected function isNewSD()
    {
        if($this->message['type']=='group_change' and
            $this->message['action']=='ADD'){
            return true;
        }else{
            return false;
        }
    }

    protected function welcomeNewSD()
    {
        $new_SD_name = $this->message['invited'];
        Text::send($this->username,$this->getWelcomeWord($new_SD_name));
    }

    private function getWelcomeWord($name)
    {
        $words = [
//            '新来的叫声爹然后滚!',
            '新来的叫大哥,以后我罩你!',
            '新来的叫爸爸,群里的规矩懂吗!',
            '新来的会装逼吗?',
            '新来的给大家磕头!',
            '新来的娇喘会吗?不会的在这个群混不下去了噢!',
            '新来的爆照,群里的规矩!',
            '大家都很忙,新来的你自己欢迎下自己吧.',
            '新来的,你,把福利交出来!',
            '大家欢迎新来的朋友.给我打.',
            '新人来了,大家快麻溜地出来欢♂迎♀[奸笑]',
            '欢迎新人,不要客气,把这当成家.你最好把钱放家里.',
            '欢迎新人,保护费交给我',
            '欢迎加入朕的后宫',
            '新来的欢迎您(又来一个傻逼)',
//            '欢迎傻逼加入,大家鼓掌!',
            '大家欢迎新来的沙雕一下',
            '新来的欢迎大家一下,不然打你!',
            '一个沙雕即将出现,大家鼓掌欢迎!'];
        $r_key = array_rand($words);
        return '@'.$name.' ,'.$words[$r_key];
    }

    /*************处理对话**************/
    protected function isSDChatMe(){
        if($this->message['type']=='text'
            and $this->message['fromType']=='Group'
            and $this->isCallMe($this->message['content'])){
            return true;
        }else{
            return false;
        }
    }

    protected function isCallMe($content){
        $res = preg_match('/^\s*(@?李)?二牛(\s)?|(@?李)?二牛(\s*|.{1,4})$/',$content);
        if(mt_rand(0,1999)<=0){$res=1;}  //随机回答
        return $res>0;
    }

    protected function chatWithSD()
    {
        if(!empty($this->message['sender']['DisplayName'])){
            $name = $this->message['sender']['DisplayName'];
        }else {
            $name = $this->message['sender']['NickName'];
        }

        if($this->message['sender']['UserName']===$this->_lastChater['username']){
            $need_at = false;
        }elseif(time()-$this->_lastChater['time']>=10){
            $this->_lastChater['username'] = $this->message['sender']['UserName'];
            $need_at = false;
        }else{
            $this->_lastChater['username'] = $this->message['sender']['UserName'];
            $need_at = true;
        }
        if($name=='王二狗'){
            $need_at = true;
        }
        $this->_lastChater['time'] = time();

        $word = $this->message['pure']??$this->message['content'];
        $word = preg_replace('/@李二牛/','',$word);
        $word = preg_replace('/(你)+(?:李)?二牛|(?:李)?二牛(你)+/','\1\2',$word);
        $word = preg_replace('/(\s|^)(李)?二牛(\s|$)/','',$word);
        $word = preg_replace('/(李)?二牛/','你',$word);
        if($word==''){
            Text::send($this->username, ($need_at?'@'.$name.' ':'').$this->getReEmptyWord());
            return;
        }
        $api = TuringApi::getObj();
        $api->setPost($word, substr($this->message['username'], 2,32), substr($this->username, 2,32), $name);
        $reContent = $api->sent();
        Text::send($this->username, ($need_at?'@'.$name.' ':'').$reContent);
    }

    protected function getReEmptyWord()
    {
        $words = [
            '叫爸爸干嘛?',
            '有屁快放!',
            '叫我干嘛,你是不是喜欢我?',
            '爸爸在呢',
            '嗯,我也爱你',
            '爱过',
            '[皱眉]',
            '[抠鼻]',
            '嘻...嘻嘻嘻',
            '嘤嘤嘤',
        ];
        $r_key = array_rand($words);
        return $words[$r_key];
    }

}